package com.yz.Queue;

import java.util.Scanner;

//将该队列改进成环形队列
//使其实现复用功能
public class RingQueue {
    public static void main(String[] args) {
        //测试
        System.out.println("这是数组模拟环形队列");
        Ring arrayQueue = new Ring(4);
        Scanner sc = new Scanner(System.in);
        char key;
        boolean a = true;
        while (a) {
            System.out.println("a(add): 为数组添加元素");
            System.out.println("r(remove): 为数组取出元素");
            System.out.println("s(show): 为数组展示所有元素");
            System.out.println("f(first): 为数组展示首数组元素");
            System.out.println("e(exit): 退出！");
            System.out.println("请输入你的指令：");
            key = sc.next().charAt(0);  //在这里接收一个字符
            switch (key) {
                case 'a':
                    System.out.println("请输入一个数：");
                    int i = sc.nextInt();
                    arrayQueue.add(i);
                    break;
                case 'r':
                    try {
                        int res = arrayQueue.remove();
                        System.out.printf("取出的数据是：%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                case 's':
                    arrayQueue.show();
                    break;
                case 'f':
                    try {
                        arrayQueue.First();
                        System.out.printf("队列的头数据是%d\n", arrayQueue.First());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    a = false;
                    break;
            }
        }
        System.out.println("退出成功！");
    }
}

class Ring {
    private int rear;
    private int front;
    private int MaxSize;
    private int[] array;


    //这是定义这个构造方法
    public Ring(int Maxsize) {
        MaxSize = Maxsize;
        rear = 0;
        front = 0;
        array = new int[MaxSize];
    }

    //1.判断是否为空
    public Boolean isNull() {
        return rear == front;
    }

    //2.判断是否为满
    public Boolean isFull() {
        return (rear + 1) % MaxSize == front;
    }

    //3.为队列添加元素
    public void add(int num) {
        if (isFull()) {
            System.out.println("该队列是满的，无法添加元素");
        } else {
            array[rear] = num;
            rear = (rear + 1) % MaxSize;
        }
    }

    //4.将队列的元素取出
    public int remove() {
        if (isNull()) {
            throw new RuntimeException("该队列是空的，无法取出元素");
        } else {
            //这里需要分析出front是指向队列的第一个元素
            //先把front对应的值保留到一个临时变量
            //将front后移，考虑取模
            //将临时保存的变量返回

            int value = array[front];
            front = (front + 1) % MaxSize;
            return value;
        }
    }

    //5.展示队列的所有元素
    public void show() {
        if (isNull()) {
            System.out.println("该队列是空的，无法展示元素");
        } else {
            for (int i = front; i < front + size(); i++) {
                System.out.printf("array[%d] = %d\n", i % MaxSize, array[i % MaxSize]);
            }
        }

    }

    //获取队列的有效数据
    public int size() {
        return (rear + MaxSize - front) % MaxSize;
    }

    //6.取出队列的首元素
    public int First() {
        if (isNull()) {
            throw new RuntimeException("该队列是空的，无法取出元素");
        } else {
            return array[front];
        }
    }
}
